﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;

public class Logs
    {
    
        public static void Write(string Msg)
        {
            try
            {
                System.Web.UI.Page page = new System.Web.UI.Page();
                string FileName =page.Server.MapPath("App_Data/Logs.log");
                string NewFileName =page.Server.MapPath("App_Data/LogsBak.log");
                int MaxLogsLines = 1000;
                int NoOfLine = 0;
                if (File.Exists(FileName))
                {
                    StreamReader sr = new StreamReader(FileName);
                    while (sr.ReadLine() != null)
                    {
                        NoOfLine = NoOfLine + 1;
                    }
                    sr.Close();


                }

                StreamWriter sw = new StreamWriter(FileName, true);

                if (NoOfLine < Convert.ToInt16(MaxLogsLines))
                {
                    sw.WriteLine(DateTime.Now.ToString() + "--" + Msg);
                    sw.Flush();
                    sw.Close();

                }
                else
                {
                    sw.WriteLine(DateTime.Now.ToString() + "--" + Msg);
                    sw.Flush();
                    sw.Close();
                    if (File.Exists(NewFileName))
                    {
                        File.Delete(NewFileName);
                    }
                    File.Move(FileName,NewFileName);

                }
            }
            catch (Exception)
            {
                
            }

        }
    }

